package com.dmzapp.api.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dmzapp.api.vo.AppBid;
import com.dmzapp.api.vo.AppInfo;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class MockAppDAO {

    private static transient Log log = LogFactory.getLog(MockAppDAO.class);

    private static SqlMapClient smc = null;

    static {

        if (smc == null) {
            Reader rd;
            try {
                rd = Resources.getResourceAsReader("SqlMapConfig.xml");
                smc = SqlMapClientBuilder.buildSqlMapClient(rd);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void mockdata() {

        String dataDir = "E:\\dmz\\招财猫APK\\招财猫APK\\apk";

        // package
        File dir = new File(dataDir);

        for (File file : dir.listFiles()) {

            String name = file.getName().substring(0, file.getName().length() - 4);
            System.out.println(name + "|" + file.length());

        }

    }

    public static void insertAppBid() {

        for (int i = 4; i < 12; i++) {
            AppBid appbid = new AppBid();
            appbid.setAppid(i);
            appbid.setServiceid(1);
            appbid.setUnit_bid(0.59);
            try {
                smc.insert("total.addAppBid", appbid);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            /*
             * appbid.setServiceid(2); appbid.setUnit_bid(0.5 + 0.1 * i); try {
             * smc.insert("total.addAppBid", appbid); } catch (SQLException e) { //
             * TODO Auto-generated catch block e.printStackTrace(); }
             */
        }
    }

    public static List<AppInfo> getAppList() {
        List<AppInfo> result = new ArrayList<AppInfo>();
        File dir = new File("E:\\dmz\\apkinfos.txt");

        BufferedReader reader = null;
        BufferedReader descReader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(dir)));
            String info = reader.readLine();
            
            while (info != null) {
                String [] array = info.split("\\|");
                AppInfo app = new AppInfo();
                app.setAppname(array[0]);
                app.setPackagename(array[1]);
                app.setVersion(array[2]);
                app.setApk_size(Long.valueOf(array[3]));
                app.setPic_number(3);
                StringBuilder desc = new StringBuilder();
                try {
                    descReader = new BufferedReader(new InputStreamReader(
                            new FileInputStream(new File("E:\\dmz\\招财猫APK\\招财猫APK\\desc\\" + array[1] + ".txt"))));
                    String line = descReader.readLine();
                    while (line != null) {
                        desc.append(line);
                        line = descReader.readLine();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                app.setDescription(desc.toString());
                result.add(app);
                info = reader.readLine();
                System.out.println(app.toString());
                
                try {
                    smc.insert("total.AddApp", app);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        
        return result;
    }

    public static void main(String[] args) {
        /*
         * File file = new File("E:\\ipad\\icon\\apk");
         * 
         * for (String name : file.list()) { System.out.println("appList.put(\"" +
         * name.substring(0, name.length() - 4) + "\", \"\");"); }
         */

        // mockdata();
        /*
         * List<AppInfo> list = AppDao.getInstance().getAllActiveApps(); for
         * (AppInfo app : list) { System.out.println(app.toString()); }
         */
        
        getAppList();
        insertAppBid();
    }
}
